﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 20/12/2009
 * Time: 20:39
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;

namespace csharp.rad.general.entries
{
	/// <summary>
	/// Description of frmRegisterSupplier.
	/// </summary>
	public partial class frmRegisterSupplier : frmRegisterPerson
	{
		Supplier supplier = new Supplier();
		Suppliers listSuppliers = new Suppliers();
		
		public frmRegisterSupplier()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			this.dgvMain.Size = new Size(808, 426);
			
			base.person = supplier.person;			
		}
		
		#region overrided methods
	
		protected override void fillDataGridView()
		{
			listSuppliers.fillList();
			
			int tam= listSuppliers.count();
			
			dgvMain.Rows.Clear();
			for (int i=0; i < tam; i++)
				dgvMain.Rows.Add(new String[] {listSuppliers.get(i).id,
				                 				listSuppliers.get(i).person.id,
					                  			 listSuppliers.get(i).person.code,
					                  			 listSuppliers.get(i).person.name, 
					                  			 listSuppliers.get(i).person.cpfCnpj
				                 });
		}
		
		protected override void find()
		{
			common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
		}
		
		protected override void init()
		{
			DataGridViewTextBoxColumn companyId = new DataGridViewTextBoxColumn();
			
			companyId.Visible = false;
			companyId.Name  = "supplierId";
			
			dgvMain.Columns.Add(companyId);	
			
			base.init();
		}
		
		protected override void newEvent()
		{
			supplier.clear();
			base.newEvent();
		}
		
		protected override bool save()
		{
			common.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
			if (!base.save())
			{
				common.Database.RollBack();
				return false;
			}
			if (!supplier.save())
			{
				common.Database.RollBack();
				return false;
			}
			common.Database.Commit();
			return true;
		}
		
		protected override void setClass()
		{
			base.setClass();
		}
		
		protected override void setEdits()
		{
			supplier.id = dgvMain.CurrentRow.Cells["supplierId"].Value.ToString();
			
			if (supplier.get())
			{
				base.setEdits();
			}
		}
		
		protected override bool update()
		{
			if (!base.update())
				return false;
			return supplier.update();
		}
		
		protected override bool validate()
		{
			return base.validate();
		}
		
		#endregion
	}
}
